#pragma once

#include "../..//base/obj_pointer.h"
#include "../../base/byte_buffer.h"
#include "../gl/texture.h"

OPEN_JLIB_NS


struct Image {

  static Texture* loadImage (ByteBuffer<>& buf);
  static Texture* loadImage (const c8* file);

  static ByteBuffer<> saveImage (Texture* texture, bool alpha, s32 quality);
  static bool saveImage (Texture* texture, bool alpha, s32 quality, const c8* file);


  static bool isPNG (const void* data, size_t size) noexcept {
    if (size <= 8)
      return false;
    auto header = *(u64*)data;
    return header == 0x0A1A0A0D474E5089UL;
  }

  static bool isJPG (const void* data, size_t size) noexcept {
    if (size <= 4)
      return false;
    auto header = *(u16*)data;
    auto footer = *(u16*)((u8*)data + size - 2);
    return header == 0xD8FF && footer == 0xD9FF;
  }
};


CLOSE_JLIB_NS


